package com.zl.tcbnewcar.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.model.CityBean;
import com.zl.tcbnewcar.model.TagEntity;
import com.zl.tcbnewcar.ui.dialog.ChooseAddressDialog;
import com.zl.tcbnewcar.ui.fragment.UsedGoodsListFragment;
import com.zl.tcbnewcar.utils.SpUtlis;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhanglei
 * @date 18/3/27
 */
public class UsedGoodsListActivity extends BaseActivity {

    @BindView(R.id.used_goods_tab)
    TabLayout usedGoodsTab;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.et_search)
    EditText etSearch;

    private List<TagEntity> tabList = new ArrayList<>();
    private ChooseAddressDialog addressDialog;
    private CityBean mCityBean;
    private String searchData;
    private UsedGoodsListFragment usedGoodsListFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_used_goods_list);
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        usedGoodsTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tabList == null || tabList.size() <= 0) {
                    return;
                }
                openUsedFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        addressDialog.setOnAddressDataListener(new ChooseAddressDialog.OnAddressDataListener() {
            @Override
            public void addressData(CityBean cityBean) {
                mCityBean = cityBean;
                tvCity.setText(cityBean.getCityName());
                search();
            }
        });
    }

    /**
     * 加载二手商品列表
     *
     * @param position
     */
    private void openUsedFragment(int position) {
        usedGoodsListFragment = UsedGoodsListFragment.newInstance(tabList.get(position).getId(), mCityBean.getCityCode());
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.used_goods_rl, usedGoodsListFragment).commit();
    }

    private void initData() {
        getTagList();
    }

    /**
     * 获取标签数据
     */
    private void getTagList() {
        new Thread() {
            @Override
            public void run() {
                String tabJson = getIntent().getStringExtra("tabJson");
                try {
                    JSONObject object = new JSONObject(tabJson);
                    if (!object.optBoolean("result")) {
                        showToast("网络有点吃力，请稍候在进入");
                        return;
                    }
                    JSONArray array = object.optJSONArray("data");
                    if (array.length() <= 0) {
                        showToast("网络有点吃力，请稍候在进入");
                        return;
                    }
                    TagEntity entity = new TagEntity();
                    entity.setDictKey("");
                    entity.setDictName("全部");
                    entity.setId(0);
                    entity.setPId(0);
                    tabList.clear();
                    tabList.add(entity);
                    for (int i = 0; i < array.length(); i++) {
                        tabList.add(new Gson().fromJson(array.optString(i), TagEntity.class));
                    }
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (TagEntity tagEntity : tabList) {
                                String dictName = tagEntity.getDictName();
                                usedGoodsTab.addTab(usedGoodsTab.newTab().setText(dictName));
                            }
                            //全部商品页
                            openUsedFragment(0);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void initView() {
        etSearch.setHint("您想要搜索的商品");
        mCityBean = SpUtlis.getCuLocationData(mActivity);
        usedGoodsTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        tvCity.setText(mCityBean.getCityName());
        addressDialog = new ChooseAddressDialog(mActivity);
    }

    @OnClick({R.id.tv_city, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_city:
                addressDialog.showDialog(view);
                break;
            case R.id.tv_search:
                search();
                break;
        }
    }

    private void search() {
        searchData = etSearch.getText().toString().trim();
        if (usedGoodsListFragment == null) return;
        usedGoodsListFragment.updateData(searchData, mCityBean != null ? mCityBean.getCityCode() : "");
    }
}
